// packageB/pages/Send_parcel/Send_parcel.js
import { ml_hideLoading, ml_showLoading, ml_showToast, ml_Toast } from '../../../utils/asyncWX'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gototime: '',   //期望上门时间
    classify_class: '', //物品信息
    means: true,  //快递类型
    address1: { 'userName': '请选择地址', 'del': '请填写清楚具体的楼栋和层数', 'telNumber': '', 'state': false }, //寄件地址
    address2: { 'userName': '请选择地址', 'del': '请填写清楚具体的楼栋和层数', 'telNumber': '', 'state': false }, //收件地址
    iconshow: false,//用户快件协议 
    express_remarks: '', //快件备注
    store_price: null,
    index: 0,
    timeduan: ['上午7:00-12:00', '中午12:30-13:30', '下午14:00-17:00', '晚餐17:30-18:30', '晚自习18:30-21:00', '晚上21:00-22:00'],
    classifyindex: null,  //商品类型索引
    classifyindex1: null,  //商品类型索引
    classify1: ['小件(<1kg)', '中小件(<2kg)', '小大件(<3kg)', '中小件(<4kg)', '中件(<5kg)', '小大件(<6kg)', '中大件(<7kg)', '大件(<8kg)', '超大件(<10kg)'],
    classify: ['文件证照', '数码产品', '美妆护肤', '服装鞋帽', '书籍资料', '饮料零食', '虚拟电子', '日用品', '其他'],
  },

  //快件备注
  textareaAInput(e) {
    this.setData({ express_remarks: e.detail.value })
  },
  //日期选择
  DateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value,
      gototime: this.data.timeduan[e.detail.value]
    })
    console.log(this.data.gototime);
  },
  //用户 快件协议
  iconargee() {
    this.setData({ iconshow: !this.data.iconshow });
  },
  //选择地址
  choose_address(e) {
    var that = this;
    let address_id = e.currentTarget.dataset.id;
    wx.chooseAddress({
      success(res) {
        console.log(res)
        //let dels = res.provinceName + res.cityName + res.countyName + res.detailInfo;
        let dels = res.detailInfo;
        if (address_id == '0') {
          that.setData({ address1: { userName: res.userName, telNumber: res.telNumber, del: dels, state: true } });
        } else if (address_id == '1') {
          that.setData({ address2: { userName: res.userName, telNumber: res.telNumber, del: dels, state: true } });
        }
      }
    })
  },
  //底部弹窗
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //底部分类弹窗选择
  classify(e) {
    let indexed = e.currentTarget.dataset.index;
    this.setData({ classifyindex1: indexed })
  },
  classify1(e) {
    let indexed = e.currentTarget.dataset.index;
    this.setData({ classifyindex: indexed })
  },
  //价格统计
  Pricecount() {
    let index1 = this.data.classifyindex1;
    let Pricecount = 0;
    switch (index1) {
      case 0: Pricecount = 2.5; break;
      case 1: Pricecount = 3.0; break;
      case 2: Pricecount = 3.5; break;
      case 3: Pricecount = 4.0; break;
      case 4: Pricecount = 4.5; break;
      case 5: Pricecount = 5.5; break;
      case 6: Pricecount = 6.5; break;
      case 7: Pricecount = 8.0; break;
      case 8: Pricecount = 10.0; break;
    }
    if (this.data.means) {
      return Pricecount;
    } else {
      return Pricecount * 2;
    }
  },
  //底部分类弹窗确认
  classifyOK() {
    if (this.data.classifyindex != null && this.data.classifyindex1 != null) {
      let classify_class = this.data.classify[this.data.classifyindex] + '(' + this.data.classify1[this.data.classifyindex1] + ')';
      this.setData({ classify_class: classify_class, store_price: this.Pricecount() })
      this.hideModal();
    } else {
      //ml_Toast('请选择完整物品信息！');
    }
  },

  //更改取件方式
  means(e) {
    let means = e.currentTarget.dataset.item;
    console.log(means)
    var that = this;
    if (means != this.data.means) {
      if (means == 0) {
        wx.showModal({
          title: '友情提示',
          content: '特快快递将由专人配送其配送费将是普通快递的几倍！承诺接单后半小时内送达！请确认选择！',
          success(res) { if (res.confirm) { that.setData({ means: false }); that.classifyOK();} }
        })
      } else { that.setData({ means: true }); that.classifyOK();}
    }
  },

  lookagreement() {
    wx.navigateTo({
      url: '/packageB/pages/login_looking/login_looking',
    })
  },

  textareaBInput(e) { this.setData({ express_remarks: e.detail.value }) },

  // 判断是否填写完整
  ifnone() {
    if (!this.data.address1.state) { return '请选择寄件地址！'; }
    if (!this.data.address2.state) { return '请选择收货地址！'; }
    if (this.data.classify_class == '') { return '请选择物品信息！'; }
    if (!this.data.iconshow) { return '请仔细阅读用户快件协议，并同意!'; }
    return 200;
  },
  //下单
  submit() {
    let ifnone = this.ifnone();
    var that = this;
    if (ifnone == 200) {
      that.order_adddatabase();

    } else {
      ml_Toast(ifnone);
    }
  },

  // 订单加入数据库
  order_adddatabase() {
    let pay_time = + new Date();  //订单创建时间
    let pay_orderinf = {
      address1: this.data.address1,
      address2: this.data.address2,
      gototime: this.data.gototime,
      classify_class: this.data.classify_class,
      means: this.data.means ? '普通快递' : '特快快递',
      express_remarks: this.data.express_remarks
    }
    ml_showLoading();
    wx.cloud.database().collection('wait_pay').add({
      data: {
        pay_time: pay_time,
        pay_orderinf: pay_orderinf,
        type_order: 'sender',
        pay_aftermoney: null,
        pay_store_weight: null,
        pay_store_inf: null,
        pay_courier_inf: null,
        rider_id: '',
        pay_courier_state: false
      }
    }).then(res => {
      console.log(res)
      if (res.errMsg == 'collection.add:ok') {
        ml_showToast('下单成功');
        this.setData({
          classify_class: '', //物品信息
          means: true,  //快递类型
          address1: { 'userName': '请选择地址', 'del': '请填写清楚具体的楼栋和层数', 'telNumber': '', 'state': false }, //寄件地址
          address2: { 'userName': '请选择地址', 'del': '请填写清楚具体的楼栋和层数', 'telNumber': '', 'state': false }, //收件地址
          iconshow: false,//用户快件协议 
          express_remarks: '', //快件备注
          index: 0,
          classifyindex: null,  //商品类型索引
          classifyindex1: null  //商品类型索引
        })
      } else {
        ml_Toast('下单失败！请重新尝试！');
      }
    }).catch(res => {
      ml_Toast('记录数据库失败！请联系客服人员！');
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取当天时间
    let nowtime = new Date().toLocaleDateString().split("/").join("-");
    this.setData({ date: nowtime, gototime: this.data.timeduan[this.data.index] });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})